package com.newcapec.erp.ui;

import com.newcapec.erp.bean.AdminEntity;
import com.newcapec.erp.main.DataInit;
import com.newcapec.erp.utils.ValidationUtils;
import com.newcapec.erp.utils.XMLFReader;
import com.newcapec.erp.utils.XMLFWriter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class PersonalDataUI extends JFrame{
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;

    public PersonalDataUI() {
        setIconImage(Toolkit.getDefaultToolkit().getImage("images/logo.png"));
        setTitle("个人信息");
        setSize(467,293);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(10, 22, 425, 27);
        String adminAn="用户账号："+ DataInit.adminEntity.getAdminAn();
        textField.setText(adminAn);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(10, 67, 425, 27);
        String adminName="姓名："+DataInit.adminEntity.getAdminName();
        textField_1.setText(adminName);
        contentPane.add(textField_1);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(10, 116, 425, 27);
        String adminTel="手机号码："+DataInit.adminEntity.getAdminTel();
        textField_2.setText(adminTel);
        contentPane.add(textField_2);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(10, 162, 425, 27);
        String adminMail="邮箱："+DataInit.adminEntity.getAdminMail();
        textField_3.setText(adminMail);
        contentPane.add(textField_3);
        textField.setEditable(false);
        textField_1.setEditable(false);
        textField_2.setEditable(false);
        textField_3.setEditable(false);

        JButton button = new JButton("修改信息");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.setEditable(true);
                textField_1.setEditable(true);
                textField_2.setEditable(true);
                textField_3.setEditable(true);
            }
        });
        button.setBounds(170, 208, 93, 23);

        contentPane.add(button);
        JButton btnNewButton = new JButton("保存");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //将获取到的字符串进行过滤去除前面的重复部分
                String text = textField.getText();
                String newAdminAn = text.replace("用户账号：", "");
                String text1 = textField_1.getText();
                String newAdminName = text1.replace("姓名：", "");
                String text2 = textField_2.getText();
                String newAdminTel = text2.replace("手机号码：", "");
                String text3 = textField_3.getText();
                String newAdminMail = text3.replace("邮箱：", "");
                //正则校验
                boolean isFlag = false;
                if(!newAdminAn.matches(ValidationUtils.NAME_REGEX)){
                    JOptionPane.showMessageDialog(null, ValidationUtils.NAME_HiNT, "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(!newAdminName.matches(ValidationUtils.NICK_REGEX)){
                    JOptionPane.showMessageDialog(null, ValidationUtils.NICK_HiNT, "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(!newAdminTel.matches(ValidationUtils.TEL_REGEX)){
                    JOptionPane.showMessageDialog(null, ValidationUtils.TEL_HiNT, "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(!newAdminMail.matches(ValidationUtils.EMAIL_REGEX)){
                    JOptionPane.showMessageDialog(null, ValidationUtils.EMAIL_HiNT, "提示",JOptionPane.WARNING_MESSAGE);
                    isFlag = true;
                }
                if(isFlag){
                    return;
                }
                AdminEntity newAdmin = new AdminEntity(newAdminAn,null,newAdminName,newAdminTel,newAdminMail);
                XMLFWriter.ChangeAdmin(newAdmin);
                JOptionPane.showMessageDialog(null, "恭喜你，修改成功.", "提示",JOptionPane.PLAIN_MESSAGE);
                dispose();
                Vector<Vector<String>> data = XMLFReader.getData(DataInit.TYPE_USER);
                MainUI.updateMainUI(DataInit.TYPE_USER,data,1);
            }
        });
        btnNewButton.setBounds(280, 208, 93, 23);
        contentPane.add(btnNewButton);
        setLocationRelativeTo(null);
        setVisible(true);
    }
}
